package com.example.demo.controller;

import com.example.demo.commom.AjaxResult;
import com.example.demo.commom.AppVariable;
import com.example.demo.commom.PasswordUtils;
import com.example.demo.commom.UserSessionUtils;
import com.example.demo.entity.Articleinfo;
import com.example.demo.entity.Userinfo;
import com.example.demo.entity.vo.UserinfoVO;
import com.example.demo.service.ArticleService;
import com.example.demo.service.UserService;
import org.apache.catalina.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:龙宝
 * Date:2023-03-31
 * Time:18:37
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private ArticleService articleService;
    @RequestMapping("/reg")
    public AjaxResult reg(Userinfo userinfo) {
        if(userinfo == null
        || !StringUtils.hasLength(userinfo.getUsername())
        || !StringUtils.hasLength(userinfo.getPassword())) {
            return AjaxResult.fail("参数错误",-1);
        }
//        System.out.println(AjaxResult.success(userService.reg(userinfo)));
        //密码加盐
        userinfo.setPassword(PasswordUtils.encrypt(userinfo.getPassword()));
        return AjaxResult.success(userService.reg(userinfo));
    }
    @RequestMapping("/login")
    public AjaxResult login(HttpServletRequest request,String username,String password) {
        if( !StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return AjaxResult.fail("参数错误",-1);
        }
        Userinfo user = userService.login(username);
        //密码加盐处理过
        if(user != null && user.getId()>0 && PasswordUtils.check(password,user.getPassword())) {
            //将用户信息存储到session中
            HttpSession session = request.getSession();//默认是true
            session.setAttribute(AppVariable.USER_SESSION_KEY,user);
            //返回前端之前，隐藏敏感信息
            user.setPassword("");
            return AjaxResult.success(user);
        } else {
            return AjaxResult.fail("用户名或密码输入错误",-1);
        }
    }

    @RequestMapping("showinfo")
    public AjaxResult showInfo(HttpServletRequest request) {
        UserinfoVO userinfoVO = new UserinfoVO();

        //1、得到当前用户（从session中获取)
        Userinfo userinfo = UserSessionUtils.getUser(request);
        System.out.println(userinfo);
        if(userinfo == null) {
            return AjaxResult.fail("非法请求",-1);
        }
        //Spring提供的深克隆
        BeanUtils.copyProperties(userinfo,userinfoVO);
        userinfoVO.setUsername(userinfo.getUsername());
        //2、得到用户的文章总数
        userinfoVO.setArtCount(articleService.getArtCountByUid(userinfo.getId()));
        System.out.println(AjaxResult.success(userinfoVO));
        return AjaxResult.success(userinfoVO);
    }
    @RequestMapping("/logout")
    public AjaxResult logout(HttpSession session) {
        session.removeAttribute(AppVariable.USER_SESSION_KEY);
        return AjaxResult.success(1);
    }
    @RequestMapping("/getuserbyid")
    public AjaxResult getUserById(Integer id) {
        //空  判断
        if(id == null || id<=0) {
            return AjaxResult.fail("非法参数",-1);
        }
        //查询用户名
        Userinfo userinfo = userService.getUserById(id);
        if(userinfo == null || userinfo.getId()<=0) {
            return AjaxResult.fail("非法参数",-1);
        }
        //设置敏感参数
        userinfo.setPassword("");
        //最终返回vo对象
        UserinfoVO userinfoVO = new UserinfoVO();
        BeanUtils.copyProperties(userinfo,userinfoVO);
        userinfoVO.setArtCount(articleService.getArtCountByUid(userinfo.getId()));
        return AjaxResult.success(userinfoVO);
    }
}


































